package model.data.container;

/**
 * CustomerContainer.java
 * Purpose: A container to hold customer objects.
 * 
 * @author Jan Harm Van Enk
 * @version 0.1 12/01/2012
 */

import java.util.ArrayList;
import java.util.List;

import model.Customer;
import model.data.CustomerStore;
import model.data.DuplicateRecordException;
import model.data.RecordNotFoundException;

public class CustomerContainer implements CustomerStore {
	/* List of customers */
	private ArrayList<Customer> customers;

	/** Constructs a new customer container */
	public CustomerContainer() {
		this.customers = new ArrayList<Customer>();
	}
	
	/** Writes an customer to the container */
	@Override
	public void write(Customer customer) throws DuplicateRecordException {
		if (this.getById(customer.getId()) == null) {
			this.customers.add(customer);
		}
		else
		{
			throw new DuplicateRecordException();
		}
	}
	
	/** Updates an existing customer in the container */
	@Override
	public void update (Customer customer) {
		// TODO update logic
		// currently impossible due to referencing issue
	}
	
	/** Deletes an existing customer from the container */
	@Override
	public void delete(Customer customer) throws RecordNotFoundException {
		if (this.getById(customer.getId()) != null) {
			this.customers.remove(customer);
		}
		else
		{
			throw new RecordNotFoundException();
		}
	}
	
	/** Returns the number of items in the container */
	@Override
	public int getCount() {
		return this.customers.size();
	}
	
	/** Returns a customer from the container with a specific id */
	@Override
	public List<Customer> getById(long id) {
		ArrayList<Customer> customers;
		
		// Create list
		customers = new ArrayList<Customer>();
		
		// Fill list
		for (Customer customer : this.customers) {
			if(customer.getId() == id){
				customers.add(customer);
			}
		}
		
		//Return the results
		return customers;
	}
}